package com.tastesport.chat.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tastesport.chat.model.redpacket.Redpacket;
import com.tastesport.chat.model.redpacket.RedpacketVO;
import com.tastesport.chat.model.redpacket.RedpacketgetVO;
import com.tastesport.chat.model.user.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface RedpacketMapper extends BaseMapper<Redpacket> {

    @Select("SELECT beans FROM user WHERE id = #{userId}")
    BigDecimal getBeans(@Param("userId") String userId);

    @Update("UPDATE chat_redpacket SET remain_num=remain_num-1, remain_money=remain_money+#{remain_money} where id=#{id}")
    Integer updateRemain(@Param("id") Integer id, @Param("remain_money") BigDecimal remain_money);

    @Update("UPDATE chat_redpacket SET best_userid=#{best_userid}, best_money=#{best_money} where id=#{id}")
    Integer updateBest(@Param("id") Integer id, @Param("best_userid") String best_userid, @Param("best_money") BigDecimal best_money);

    @Update("UPDATE chat_redpacket_get SET isbest=#{isbest} WHERE redpacket_id=#{redpacketId} AND get_userid=#{userId}")
    Integer changeBest(@Param("redpacketId") Integer redpacketId, @Param("userId") String userId, @Param("isbest") Integer isbest);

    @Update("UPDATE user SET beans=beans+(#{money}) WHERE id=#{userId}")
    Integer changeMoney(@Param("userId") String userId, @Param("money") BigDecimal money);

    @Update("UPDATE chat_redpacket set state=#{state} WHERE id=#{id}")
    Integer setState(@Param("id") Integer id, @Param("state") Integer state); // 设置红包状态

    @Select("SELECT r.*,u.nickname userName,u.user_icon userIcon FROM chat_redpacket r INNER JOIN user u WHERE r.user_id=u.id" +
            " AND r.id = #{redpacketId}")
    RedpacketVO getRedpacketById(Integer redpacketId);

    @Select("SELECT * FROM user WHERE id=#{id}")
    User getUser(String id);
}
